package cn.tedu.controller;


import cn.tedu.pojo.Item;
import cn.tedu.service.ItemService;
import cn.tedu.vo.JsonResult;
import cn.tedu.vo.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/item")
@Api(tags = {"商品接口"})
public class ItemController {



    @Autowired
    private ItemService itemService;

    @GetMapping("/listPage")
    public JsonResult findItemsPage(Item item,String pageSize,String currentSize){
        PageInfo<Item> list = itemService.pageList(item,pageSize,currentSize);
        return JsonResult.success("分页查询成功",list);
    }

    @GetMapping("/list")
    public JsonResult findItems(Item item){
        //QueryWrapper<Item> wrapper = new QueryWrapper<>();
        //wrapper.eq(StringUtils.isBlank(item.getTitle()),"title",item.getTitle()).eq()
        List<Item> list = itemService.list();
        return JsonResult.success("处理成功",list);
    }

    @GetMapping("/findItemById")
    public JsonResult findItemById(String id){
        Item i = itemService.findItemById(id);
        return JsonResult.success("处理成功",i);
    }

    @PostMapping
    public JsonResult addItem(Item item){
        itemService.save(item);
        return JsonResult.success("处理成功");
    }

    @PutMapping
    public JsonResult updateItem(@RequestBody Item item){
        Item i = itemService.updateItemById(item);
        return JsonResult.success("处理成功");
    }

    @DeleteMapping
    public JsonResult deleteItemById(String... ids){
        System.out.println(ids);
        for (String id :ids){
            itemService.removeItemById(id);
        }
        return JsonResult.success("处理成功");
    }

}
